import express from "express";
// import { addUser,getUser } from "./data/users"; 
import { routes } from './routes'
import { addTodo,getAllTodo } from "./data/todo"; 
const app = express();
const PORT = 3000;
import {connection} from './data/db'

app.use(express.urlencoded({ extended: false }));
app.use(express.json()); // for parsing application/json


// app.use(statics(path.join(__dirname, staticPath))); 
// 跨域
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    next();
});

routes.forEach((route)=>{
    const {method, path, middleware, handler} = route;
    app [method](path,...middleware,handler);
});

app.post('/todo',async(req,res)=>{
    console.log(req.body);

    await addTodo({id:0,
        content:req.body.content,
        status:req.body.status
    })

    return res.status(200).send({
        status:'success'
    });

})
app.get('/todo',async(req, res)=>{
    let _o = await getAllTodo()

    return res.status(200).send(_o);
})

// app.get('/db',async (req,res)=>{

 
//     const _o = await getInformation();

//     // console.log(_o)
//     // const rows = await query('select * from app_groups',[]);
//     // console.log(rows);
//     // await getInfomation();
//     res.send(_o);
// })





app.listen(PORT, ()=>{
    console.log(`Express with Typescript! http://localhost:${PORT}`);
})